package com.dmbjz.rpc.config;


import com.dmbjz.rpc.service.WeatherInterface;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

@Configuration
public class WeatherConfig {


    /** 创建 HttpInterface 需要的工厂 */
    @Bean
    HttpServiceProxyFactory httpInterfaceFactory() {

        //1、创建客户端
        WebClient client = WebClient.builder()
                .codecs(clientCodecConfigurer -> {
                    clientCodecConfigurer
                            .defaultCodecs()
                            .maxInMemorySize(256 * 1024 * 1024);
                    //响应数据量太大有可能会超出BufferSize，所以这里设置的大一点
                })
                .build();

        //2、创建工厂
        return HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();

    }


    /**
     * 将 WeatherInterface 注入
     */
    @Bean
    WeatherInterface weatherInterface(HttpServiceProxyFactory httpInterfaceFactory) {

        return httpInterfaceFactory.createClient(WeatherInterface.class);

    }


}
